package grpc

import (
	"context"
	grpc "github.com/go-kit/kit/transport/grpc"
	context1 "golang.org/x/net/context"
	endpoint "mytest/kitcli/user/pkg/endpoint"
	"mytest/kitcli/user/pkg/transport/grpc/pb"
)

// makeGetUserListHandler creates the handler logic
func makeGetUserListHandler(endpoints endpoint.Endpoints, options []grpc.ServerOption) grpc.Handler {
	return grpc.NewServer(endpoints.GetUserListEndpoint, decodeGetUserListRequest, encodeGetUserListResponse, options...)
}

// decodeGetUserListResponse is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain GetUserList request.
// TODO implement the decoder
func decodeGetUserListRequest(_ context.Context, r interface{}) (interface{}, error) {
	req := r.(*pb.GetUserListRequest)
	return endpoint.GetUserListRequest{Request: req}, nil
}

// encodeGetUserListResponse is a transport/grpc.EncodeResponseFunc that converts
// a user-domain response to a gRPC reply.
// TODO implement the encoder
func encodeGetUserListResponse(_ context.Context, r interface{}) (interface{}, error) {
	resp := r.(endpoint.GetUserListResponse)
	if resp.Err != nil {
		return nil, resp.Err
	}
	return resp.Result, nil
}
func (g *grpcServer) GetUserList(ctx context1.Context, req *pb.GetUserListRequest) (*pb.GetUserListReply, error) {
	_, rep, err := g.getUserList.ServeGRPC(ctx, req)
	if err != nil {
		return nil, err
	}
	return rep.(*pb.GetUserListReply), nil
}

// makeAddUserHandler creates the handler logic
func makeAddUserHandler(endpoints endpoint.Endpoints, options []grpc.ServerOption) grpc.Handler {
	return grpc.NewServer(endpoints.AddUserEndpoint, decodeAddUserRequest, encodeAddUserResponse, options...)
}

// decodeAddUserResponse is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain AddUser request.
// TODO implement the decoder
func decodeAddUserRequest(_ context.Context, r interface{}) (interface{}, error) {
	req := r.(*pb.AddUserRequest)
	return endpoint.AddUserRequest{Request: req}, nil
}

// encodeAddUserResponse is a transport/grpc.EncodeResponseFunc that converts
// a user-domain response to a gRPC reply.
// TODO implement the encoder
func encodeAddUserResponse(_ context.Context, r interface{}) (interface{}, error) {
	resp := r.(endpoint.AddUserResponse)
	return resp.Result, nil
}
func (g *grpcServer) AddUser(ctx context1.Context, req *pb.AddUserRequest) (*pb.AddUserReply, error) {
	_, rep, err := g.addUser.ServeGRPC(ctx, req)
	if err != nil {
		return nil, err
	}
	return rep.(*pb.AddUserReply), nil
}

// makeUpdateUserHandler creates the handler logic
func makeUpdateUserHandler(endpoints endpoint.Endpoints, options []grpc.ServerOption) grpc.Handler {
	return grpc.NewServer(endpoints.UpdateUserEndpoint, decodeUpdateUserRequest, encodeUpdateUserResponse, options...)
}

// decodeUpdateUserResponse is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain UpdateUser request.
// TODO implement the decoder
func decodeUpdateUserRequest(_ context.Context, r interface{}) (interface{}, error) {
	req := r.(*pb.UpdateUserRequest)
	return endpoint.UpdateUserRequest{Request: req}, nil
}

// encodeUpdateUserResponse is a transport/grpc.EncodeResponseFunc that converts
// a user-domain response to a gRPC reply.
// TODO implement the encoder
func encodeUpdateUserResponse(_ context.Context, r interface{}) (interface{}, error) {
	resp := r.(endpoint.UpdateUserResponse)
	return resp.Result, nil
}
func (g *grpcServer) UpdateUser(ctx context1.Context, req *pb.UpdateUserRequest) (*pb.UpdateUserReply, error) {
	_, rep, err := g.updateUser.ServeGRPC(ctx, req)
	if err != nil {
		return nil, err
	}
	return rep.(*pb.UpdateUserReply), nil
}

// makeDeleteUserHandler creates the handler logic
func makeDeleteUserHandler(endpoints endpoint.Endpoints, options []grpc.ServerOption) grpc.Handler {
	return grpc.NewServer(endpoints.DeleteUserEndpoint, decodeDeleteUserRequest, encodeDeleteUserResponse, options...)
}

// decodeDeleteUserResponse is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain DeleteUser request.
// TODO implement the decoder
func decodeDeleteUserRequest(_ context.Context, r interface{}) (interface{}, error) {
	req := r.(*pb.DeleteUserRequest)
	return endpoint.DeleteUserRequest{Request: req}, nil
}

// encodeDeleteUserResponse is a transport/grpc.EncodeResponseFunc that converts
// a user-domain response to a gRPC reply.
// TODO implement the encoder
func encodeDeleteUserResponse(_ context.Context, r interface{}) (interface{}, error) {
	resp := r.(endpoint.DeleteUserResponse)
	return resp.Result, nil
}
func (g *grpcServer) DeleteUser(ctx context1.Context, req *pb.DeleteUserRequest) (*pb.DeleteUserReply, error) {
	_, rep, err := g.deleteUser.ServeGRPC(ctx, req)
	if err != nil {
		return nil, err
	}
	return rep.(*pb.DeleteUserReply), nil
}

// makeDeleteUserHandler creates the handler logic
func makeFindUserHandler(endpoints endpoint.Endpoints, options []grpc.ServerOption) grpc.Handler {
	return grpc.NewServer(endpoints.FindUserEndpoint, decodeFindUserRequest, encodeFindUserResponse, options...)
}

// decodeDeleteUserResponse is a transport/grpc.DecodeRequestFunc that converts a
// gRPC request to a user-domain DeleteUser request.
// TODO implement the decoder
func decodeFindUserRequest(_ context.Context, r interface{}) (interface{}, error) {
	req := r.(*pb.FindUserRequest)
	return endpoint.FindUserRequest{Request: req}, nil
}

// encodeDeleteUserResponse is a transport/grpc.EncodeResponseFunc that converts
// a user-domain response to a gRPC reply.
// TODO implement the encoder
func encodeFindUserResponse(_ context.Context, r interface{}) (interface{}, error) {
	resp := r.(endpoint.FindUserResponse)
	return resp.Result, nil
}
func (g *grpcServer) FindUser(ctx context1.Context, req *pb.FindUserRequest) (*pb.FindUserReply, error) {
	_, rep, err := g.findUser.ServeGRPC(ctx, req)
	if err != nil {
		return nil, err
	}
	return rep.(*pb.FindUserReply), nil
}
